Create new users using Invoke-RestMethod powershell

I am trying to create a script that will create users in our account, so I don’t have to create massive number of accounts manually. Currently, I have a list of almost 200 that need to be created. I can successfully GET our user account info using the following, and it works just fine:

$APIKey=“Token token=[REMOVED]”
$URI0 = “URL”
$URI1 = “

$PDoutput1=Invoke-RestMethod -Method Get -Uri $URI0 -Header @{“Authorization”=$APIKey; “Accept” = "application/vnd.pagerduty+json;version=2”}
$PDoutput2=Invoke-RestMethod -Method Get -Uri $URI1 -Header @{“Authorization”=$APIKey; “Accept” = "application/vnd.pagerduty+json;version=2”}

Now to CREATE users, I put this together:

$APIKey= “Token token=[REMOVED]”
$URI0 = “URL (said I can only have 2 links in a post)”

#User Info
$name = “”
$email = “”

#Role name in PD and what they mean
#Manager = user
#Responder = limited_user
#Observer = observer
#Global Admin = admin
#Account Owner = owner

$role = “user”

$header = @"
"Authorization"= "$APIKey"
"Accept" = "application/vnd.pagerduty+json;version=2

"From" = "<my email address>"

"user": {
"type": "user",
"name": "$name",
"email": "$email",
"role": "$role"

Invoke-RestMethod -Method Post -Uri $URI0 -Headers $header

And the resulting error:

Invoke-RestMethod : Cannot bind parameter ‘Headers’. Cannot convert the "{
“Authorization”= “Token token=[Removed]”
“Accept” = "application/vnd.pagerduty+json;version=2”

“From” = ""
“user”: {
“type”: “user”,
“name”: “”,
“email”: “Email address”,
“role”: “user”
}" value of type “System.String” to type “System.Collections.IDictionary”.
At line:36 char:52

  • Invoke-RestMethod -Method Post -Uri $URI0 -Headers $header
  •                                                ~~~~~~~
    • CategoryInfo : InvalidArgument: (:slight_smile: [Invoke-RestMethod], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


Have you checked out his doc here on importing users?

Kind regards,

Hi John,

Thanks for that. I will definitely review it. I was hoping to get this figured out with powershell since I have never touched Ruby, and Powershell is what I am learning to work from and use the most in my position.

The error itself is because you’re sending a string literal for the header when it requires a dictionary. You’re also attempting to send the body of the request as a header.
Give this a try:

$APIKey= “Token token=[REMOVED]”
$URI0 = “”

$headers = @()

$body = @{
type = “user”
name = “John Doe”
email = ""
role = “admin”

$jsonBody = $body | ConvertTo-Json

Invoke-RestMethod -Method Post -Uri $URI0 -Body $jsonBody

Thanks Christopher! This is exactly what I ended up doing. I asked for assistance from someone on our server team and he grabbed the example code from the API reference page using curl, then posted it into postman and changed the language to Powershell. Cool little tool!

Here is the full code I am using, just in case anyone else is wanting to use Powershell to handle this. I was successfully able to upload around 200 users with this script. Just make sure you don’t switch networks half way through otherwise it yells at you with errors… Some reporting was added as well. Excel file to show any errors, as well as responses received.





“user”: {
“type”: “user”,
“name”: “Full Name”,
“email”: “”,
“role”: “user”
read-host -assecurestring -prompt “here:” | Export-clixml -path .\apikey.xml

$pdLocation = “<path”

#Prompt to enter API key and save it encrypted
#read-host -assecurestring -prompt “here” | Export-clixml -path “”

$apikey = Import-Clixml “” | ConvertFrom-SecureString -AsPlainText
$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$headers.Add(“Content-Type”, “application/json”)
$headers.Add(“Accept”, “application/vnd.pagerduty+json;version=2”)
$headers.Add(“From”, “”)
$headers.Add(“Authorization”, “Token token=$apikey”)

function new-pgUser {
param ()
$body = "{n"user": {n "type": "user",n“name":”$($user.worker)",n "email": "$($",n“role":"user"n }`n}”
$response = Invoke-RestMethod ‘’ -Method ‘POST’ -Headers $headers -Body $body -ErrorVariable userError
$usererror | add-member noteproperty user($
$script:userErrors += $usererror



$pdUsers = import-csv “\PD_Users_Upload_List.csv”
$responses = foreach ($user in $pdUsers){new-pgUser}

$responses.user | ConvertTo-Csv | out-file -Path “\responses.csv” -Force
$usererrors | Select-Object user,ErrorRecord,message | ConvertTo-Csv | out-file -Path “\errors.csv” -Force

Apparently “New Users” can’t post any links at all, not even the API link. So I had to edit the post a bit.